 /*
 
This program runs the premia regs to make Table 2, Table A7, Figure 1

*/
 
 
 
 
 **Set directories
cd ""  /* PROJECT ROOT FOLDER */

/* DEFINE GLOBALS PATHS HERE */
global data data
global temp work
global output output
global distances "data/distances"
global disc_programs code
global disclosure code


*counts of the number of manufacturing affiliate countries
use "$data/country_imp_aff2007.dta", clear


gen man_aff_countries=1 if man_aff_sales>0 & man_aff_sales~=.
foreach vv in exp imp {
	gen num_`vv'ort_ctrs=1 if `vv'orter==1
	gen num_`vv'ort_ctrs_naff=1 if `vv'orter==1 & man_aff_countries~=1
	gen `vv'orts_noaff=`vv'_value if man_aff_countries~=1
	}

collapse (sum) man_aff_countries num_export_ctrs* num_import_ctrs* imports_noaff exports_noaff, by(firmid)
save $data/man_aff_countries.dta, replace

*counts of the number of AL import countries
use $data/country_imp_aff2007.dta, clear
keep firmid iso imp_value rp_imp_value exp_value rp_exp_value

foreach vv in imp exp {
	replace `vv'_value=0 if `vv'_value==.	
	replace rp_`vv'_value=0 if rp_`vv'_value==.
	gen al_`vv'=`vv'_value-rp_`vv'_value
	gen al_`vv'_countries=1 if al_`vv'>0
	label variable al_`vv'_countries "Number of countries with AL `vv'ports"
	}

collapse (sum) al_imp_countries al_exp_countries al_imp al_exp, by(firmid)
save $data/al_countries.dta, replace



 use "$data/firm_2007_withtypes.dta", clear 
 
 keep if substr(type,1,3)=="MAN"
 
 merge 1:1 firmid using $data/man_aff_countries.dta
 drop if _merge==2
 drop _merge
 merge 1:1 firmid using $data/al_countries.dta
 drop if _merge==2
 
*construct variables
tab mne_status

*Simplified mne_status 
replace mne_status="DOM" if mne_status==""
replace mne_status="FOR" if mne_status=="FO2" 
replace mne_status="DOMIMP" if mne_status=="DOM" & importer==1
replace mne_status = "MNEFORMAN" if foreign_manuf ==1 & mne_status =="MNE"


* number of trade countries
gen log_imp_countries=ln(number_import_countries)
gen log_exp_countries=ln(number_export_countries)
gen log_imp_ctrs_noaff=ln(num_import_ctrs_naff)
gen log_exp_ctrs_noaff=ln(num_export_ctrs_naff)
gen log_al_imp_ctrs=ln(al_imp_countries)
gen log_al_exp_ctrs=ln(al_exp_countries)

pwcorr num_export_ctrs number_export_countries
pwcorr num_import_ctrs number_import_countries

gen m_imp=1 if number_import_countries>=2 & number_import_countries~=.
gen m_exp=1 if number_export_countries>=2 & number_export_countries~=.

label variable m_imp



*number of plants
gen log_man_affs=ln(man_aff)
  gen log_man_affs2=log_man_affs
    replace log_man_affs2=0 if log_man_affs==.
    
    /*
    Confirmed that this change has no material impact on the estimates
    Leave as is to minimize disclosure avoidance concerns
    replace log_man_affs2=0 if mne_status=="FOR" 
    */

replace number_of_aff=0 if number_of_aff==.
gen log_all_estabs=ln(estabs+number_of_aff)
gen log_estabs=ln(estabs)

gen mne=1 if man_aff>0 & man_aff~=.
  replace mne=0 if man_aff==0 | man_aff==.

gen one=1
egen source_countries=rowtotal(one number_import_countries)
egen sale_countries=rowtotal(one number_export_countries)
gen log_source_countries=ln(source_countries)
gen log_sale_countries=ln(sale_countries)
gen log_assemble_countries=ln(1+man_aff_countries)

*impute missing manuf sales to avoid disclosure problems
gen ratio=sales_man/emp_man
summ ratio, d
local rt=r(p50)
replace sales_man=`rt'*emp_man if sales_man==0


*firm size
replace aff_sales=0 if aff_sales==.
gen sales_world=sales+aff_sales
gen log_sales_world=ln(sales+aff_sales)
gen log_sales=ln(sales)
gen log_emp=ln(emp)
gen log_man_sales=ln(sales_man)

gen log_inds=log(unique)

*firm age control
xtile age_cat=firmage, nq(5)

tab age_cat, miss

replace age_cat=6 if age_cat==.  // avoid sample problems from missing age

*firm productivity
gen log_prod=ln(sales/emp)
gen log_prod_man=ln(sales_man/emp_man)

*prof share
gen share_prof=(emp_manage+emp_prof)/emp
gen share_man=(emp_man)/emp

gen share_us_man=emp_man/(emp_man+man_aff_emp)


*trade intensity measures
gen log_imports_sales=ln(imp_value/sales)
gen log_exports_sales=ln(exp_value/sales)
gen log_imports=ln(imp_value)
gen log_exports=ln(exp_value)
gen log_imports_noaff=ln(imports_noaff)
gen log_exports_noaff=ln(exports_noaff)
gen log_imports_al=ln(al_imp)
gen log_exports_al=ln(al_exp)

*separate importing, mne, rp_trader
gen rp_importer_also = (rp_importer==1 & mne_status=="DOMIMP")
gen al_importer_only = (rp_importer==0 & mne_status=="DOMIMP" & importer==1)

gen rp_exporter_also = (rp_exporter==1 & (mne_status=="DOMIMP" | mne_status=="DOM"))
gen al_exporter_only = (rp_exporter==0 &  exporter==1 &(mne_status=="DOMIMP"  | mne_status=="DOM"))

*make a categorical variable

gen global_stat_imp=0 if exporter==0 & (mne_status=="DOM" | mne_status=="DOMIMP")
	replace global_stat_imp=1 if exporter==1 & rp_exporter==0 & (mne_status=="DOM" | mne_status=="DOMIMP")
	replace global_stat_imp=2 if rp_exporter==1 & (mne_status=="DOM" | mne_status=="DOMIMP")
	replace global_stat_imp=3 if mne_status=="FOR"
	replace global_stat_imp=4 if mne_status=="MNE"
	replace global_stat_imp=5 if mne_status=="MNEFORMAN"
tab global_stat_imp, miss

gen global_stat_exp=0 if importer==0 & (mne_status=="DOM")
	replace global_stat_exp=1 if importer==1 & rp_importer==0 & mne_status=="DOMIMP"
	replace global_stat_exp=2 if rp_importer==1 & mne_status=="DOMIMP"
	replace global_stat_exp=3 if mne_status=="FOR"
	replace global_stat_exp=4 if mne_status=="MNE"
	replace global_stat_exp=5 if mne_status=="MNEFORMAN"
tab global_stat_exp, miss



label define gstat_imp 0 "Domestic" 1 "AL Exporter Only" 2 "RP Exporter Also" 3 "Foreign" 4 "US MNE"  5 "US MNE with FManuf"
label values global_stat_imp gstat_imp
label define gstat_exp 0 "Domestic" 1 "AL Importer Only" 2 "RP Importer Also" 3 "Foreign" 4 "US MNE"  5 "US MNE with FManuf"
label values global_stat_exp gstat_exp

*MNE indicators
gen mne_bjrs= rp_exporter==1 | rp_importer==1

tab mne_status mne_bjrs, miss

gen mne_compare="BJRS" if mne_bjrs==1 & mne_status=="DOM" | mne_status=="DOMIMP"
	replace mne_compare="BOTH M" if mne_bjrs==1 & (mne_status=="FOR"  | mne_status=="MNEFORMAN" | mne_status=="MNE")
	replace mne_compare="AFFT" if  mne_bjrs==0 & (mne_status=="FOR"  |  mne_status=="MNE" | mne_status=="MNEFORMAN")
	replace mne_compare="DOM" if mne_compare==""
tab mne_compare mne_status, miss
	
gen mne_simp="MNE" if mne_status=="MNEFORMAN"
  replace mne_simp="FOR" if mne_status=="FOR"
  replace mne_simp="DOM" if mne_simp==""
 
 
xi i.mne_status i.mne_compare i.global_stat_exp i.global_stat_imp i.age_cat, noomit

label variable _Iglobal_sta1 "AL Exporter Only"
label variable _Iglobal_sta2 "RP Exporter Also"
label variable _Iglobal_st_1 "AL Importer Only"
label variable _Iglobal_st_2 "RP Importer Also"
label variable _Iglobal_st_3 "Foreign MNE"
label variable _Iglobal_st_4 "US MNE"
label variable _Iglobal_st_5 "US MNE w/For Manuf"

gen mne_stat2=mne_simp 
  replace mne_stat2="MNE MUS" if mne_status=="MNE"
 
*I WANT THIS FOR THE JEP 
tabstat share_man share_us_man, by(mne_stat2) stats(p25 p50 p75 mean)


estimates clear


*Firms Size and Productivity premia table


areg log_emp _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iage*, a(naics4)
  capture drop sample_firm_premia
  gen sample_firm_premia = e(sample)
  label variable sample_firm_premia "Sample for the Firm Premia Regressions"
 do "$disclosure/count_regs.do"
estimates store log_emp1
areg log_emp  _Iglobal_st_1 _Iglobal_st_2 _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iglobal_sta1 _Iglobal_sta2  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_emp2
areg log_sales _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_sales1
areg log_sales  _Iglobal_st_1 _Iglobal_st_2 _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iglobal_sta1 _Iglobal_sta2  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_sales2
areg log_sales_world  _Iglobal_st_1 _Iglobal_st_2 _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iglobal_sta1 _Iglobal_sta2  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_sales_world
test _Iglobal_st_3 = _Iglobal_st_5
test _Iglobal_st_4 = _Iglobal_st_5

areg log_man_sales _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_sales_man
areg log_man_sales _Iglobal_st_1 _Iglobal_st_2 _Iglobal_sta1 _Iglobal_sta2  _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_sales_man2

areg log_prod _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_prod1
areg log_prod _Iglobal_st_1 _Iglobal_st_2 _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iglobal_sta1 _Iglobal_sta2  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_prod2
test _Iglobal_st_3 = _Iglobal_st_5
test _Iglobal_st_4 = _Iglobal_st_5

areg log_prod_man _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_prod_man1
areg log_prod_man _Iglobal_st_1 _Iglobal_st_2 _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iglobal_sta1 _Iglobal_sta2  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store log_prod_man2

areg share_prof _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store share_prof1
areg share_prof _Iglobal_st_1 _Iglobal_st_2 _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 _Iglobal_sta1 _Iglobal_sta2  _Iage*, a(naics4)
 do "$disclosure/count_regs.do"
estimates store share_prof2

*Make APPENDIX TABLE A.7
esttab log_emp2 log_sales2 log_prod2 log_sales_man2 log_prod_man2  using "$output/TABLE_A7.xls", replace title("Size Premia Estimates sheet=premia_size_regs") ///
   cells(b(star fmt(%9.3f)) se(par))  ///
   drop(_cons _Iage*) ///
   order(_Iglobal_st_1 _Iglobal_st_2 _Iglobal_sta1 _Iglobal_sta2  _Iglobal_st_3 _Iglobal_st_4 _Iglobal_st_5 )  ///
   stats(r2_a N N_round , fmt(%9.3f %9.0fc %9.0fc ) ///
   labels("Adj. R2" "Observations" "Observations")) ///
   starlevels(* 0.10 ** 0.05 *** 0.01) label tab  
******************************************************************



**COUNTRY TRADE MARGINS****
******************************************************************
estimates clear

**Limit to 2+ country traders (here and for regressions)


*Number of countries and amount of trade
areg log_imp_countries log_sales log_estabs _Iglobal_st_3  _Iglobal_st_5  if m_imp==1, a(naics4_primary)
  capture drop sample_imp_premia
  gen sample_imp_premia=e(sample)
  label variable sample_imp_premia "Sample for the Import Premia Regressions"
  do "$disclosure/count_regs.do"
estimates store log_imp_countries1
areg log_imp_countries log_sales log_estabs _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_imp==1, a(naics4_primary)
  do "$disclosure/count_regs.do"
estimates store log_imp_countries2
areg log_imp_countries log_sales log_estabs log_inds _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_imp==1, a(naics4_primary)
  do "$disclosure/count_regs.do"
estimates store log_imp_countries3
areg log_imp_countries log_sales log_estabs log_inds _Iglobal_sta1 _Iglobal_sta2  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_imp==1, a(naics4_primary)
  do "$disclosure/count_regs.do"
 estimates store log_imp_countries4
 
*AL imports only (drop foreign firms) 
areg log_al_imp_ctrs log_sales log_estabs  log_inds _Iglobal_st_5 log_man_affs2 if m_imp==1 & _Iglobal_st_3==0, a(naics4_primary)
  capture drop sample_imp_premia_al
  gen sample_imp_premia_al=e(sample)
  do "$disclosure/count_regs.do"
  label variable sample_imp_premia_al "Sample for the AL Import Premia Regressions"
  estimates store log_al_imp_countries1


areg log_imports log_sales log_estabs _Iglobal_st_3 _Iglobal_st_5 if m_imp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_imports1
areg log_imports log_sales log_estabs _Iglobal_st_3 _Iglobal_st_5 log_man_affs2  if m_imp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_imports2
areg log_imports log_sales log_estabs log_inds _Iglobal_st_3 _Iglobal_st_5 log_man_affs2  if m_imp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_imports3
areg log_imports log_sales log_estabs log_inds _Iglobal_sta1 _Iglobal_sta2  _Iglobal_st_3 _Iglobal_st_5 log_man_affs2  if m_imp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_imports4

areg log_imports_al log_sales log_estabs log_inds _Iglobal_st_5 log_man_affs2  if m_imp==1 & _Iglobal_st_3==0 , a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_al_imports1

*Check AL complement samples
tab sample_imp_premia sample_imp_premia_al, miss

tab global_stat_imp if sample_imp_premia==1 & sample_imp_premia_al==0, miss


areg log_exp_countries log_sales log_estabs _Iglobal_st_3  _Iglobal_st_5 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
capture drop sample_exp_premia
  gen sample_exp_premia=e(sample)
  label variable sample_exp_premia "Sample for the Export Premia Regressions"
estimates store log_exp_countries1
areg log_exp_countries log_sales log_estabs  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exp_countries2
areg log_exp_countries log_sales log_estabs  log_inds  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exp_countries3
areg log_exp_countries log_sales log_estabs log_inds  _Iglobal_st_1 _Iglobal_st_2  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exp_countries4

*AL exports
areg log_al_exp_ctr log_sales log_estabs  log_inds _Iglobal_st_5  log_man_affs2 if m_exp==1 &  _Iglobal_st_3 ==0, a(naics4_primary)
capture drop sample_exp_premia_al
  gen sample_exp_premia_al=e(sample)
  label variable sample_exp_premia_al "Sample for the AL Export Premia Regressions"
 do "$disclosure/count_regs.do"
estimates store log_al_exp_countries1


areg log_exports log_sales log_estabs  _Iglobal_st_3  _Iglobal_st_5  if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exports1
areg log_exports log_sales log_estabs  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exports2
areg log_exports log_sales log_estabs  log_inds _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exports3
areg log_exports log_sales log_estabs  log_inds _Iglobal_st_1 _Iglobal_st_2  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2 if m_exp==1, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_exports4


areg log_exports_al log_sales log_estabs  log_inds _Iglobal_st_5  log_man_affs2 if m_exp==1 & _Iglobal_st_3==0, a(naics4_primary)
 do "$disclosure/count_regs.do"
estimates store log_al_exports1

*Check AL complement samples
tab sample_imp_premia sample_imp_premia_al, miss

tab global_stat_imp if sample_imp_premia==1 & sample_imp_premia_al==0, miss


*Make TABLE 2
esttab log_imp_countries1 log_imp_countries3 log_al_imp_countries1 log_exp_countries1 log_exp_countries3 log_al_exp_countries1 using "$output/TABLE_2.xls", /// 
  replace title("TABLE 2 -- NUMBER OF COUNTRIES") ///
   cells(b(star fmt(%9.3f)) se(par))  ///
   keep(_Iglobal_st_3  _Iglobal_st_5 log_man_affs2) ///
   order(log_sales log_estabs  log_inds _Iglobal_sta1 _Iglobal_sta2 _Iglobal_st_1 _Iglobal_st_2  _Iglobal_st_3 _Iglobal_st_5  log_man_affs2) /// 
   stats(r2_a N N_round , fmt(%9.3f %9.0fc %9.0fc ) ///
   labels("Adj. R2" "Observations" "Observations")) ///
   starlevels(* 0.10 ** 0.05 *** 0.01) label tab  
   
esttab log_imports1 log_imports3 log_al_imports1 log_exports1 log_exports3 log_al_exports1   using "$output/TABLE_2.xls", ///
  append title("TABLE 2 -- LOG OF IMPORTS") ///
   cells(b(star fmt(%9.3f)) se(par))  ///
   keep(_Iglobal_st_3  _Iglobal_st_5 log_man_affs2) ///
   order(log_sales log_estabs  log_inds _Iglobal_sta1 _Iglobal_sta2 _Iglobal_st_1 _Iglobal_st_2  _Iglobal_st_3  _Iglobal_st_5  log_man_affs2) /// 
   stats(r2_a N N_round , fmt(%9.3f %9.0fc %9.0fc ) ///
   labels("Adj. R2" "Observations" "Observations")) ///
   starlevels(* 0.10 ** 0.05 *** 0.01) label tab     
***********************************  


*FIGURE 2
*PREMIA LINCOMS and FIGURES
*************************
*define indicators
foreach val in imp exp {
	forvalues i=1/25 {
		gen `val'_d07_`i'=1 if number_`val'ort_countries>=`i' & number_`val'ort_countries~=.
		replace `val'_d07_`i'=0 if number_`val'ort_countries<`i' | number_`val'ort_countries==.
		}
		}	
		
	
*lincoms
capture program drop make_fig
program define make_fig
   args reg_var vname eval 

*Define zero and one country
 capture drop graph07_`vname' 
 capture drop se07_`vname'
  gen double graph07_`vname'=0 if _n==1
  gen double se07_`vname'=0 if _n==1

  replace graph07_`vname'=_b[`reg_var'_d07_1]  if _n==2
  replace se07_`vname'=_se[`reg_var'_d07_1]  if _n==2
  
*Loop over all subsequent combinations
local t="`reg_var'_d07_1"
 forvalues i=2/`eval' {
    local j=`i'+1
    local t="`t'+" + "`reg_var'_d07_`i'"
    di "`t'"
    lincom "`t'"
	replace graph07_`vname'=r(estimate) if _n==`j'
	replace se07_`vname'=r(se) if _n==`j'
	}
 capture drop `vname'_CI07_l
  gen `vname'_CI07_l=graph07_`vname'-1.96*se07_`vname'
  capture drop `vname'_CI07_u
  gen `vname'_CI07_u=graph07_`vname'+1.96*se07_`vname'
  
  capture drop Num_Countries_`vname'
  gen Num_Countries_`vname'=_n if se07_`vname'~=.
  
  end
  
*Imports
*********************** 
*replicate aft figure 
areg log_sales imp_d07*, a(naics4) 
make_fig imp imp 25 
  
*control for mne status	
areg log_sales imp_d07* _Imne_statu_3 _Imne_statu_5, a(naics4)
make_fig imp imp_mne 25
*********************** 

*Exports
*********************** 
*replicate aft figure 
areg log_sales exp_d07*, a(naics4) 
make_fig exp exp 25 
  
*control for mne status	
areg log_sales exp_d07* _Imne_statu_3 _Imne_statu_5, a(naics4)
make_fig exp exp_mne 25
*********************** 



*Import premia 
quietly {
#delimit ;
  twoway rarea graph07_imp imp_CI07_l Num_Countries_imp, color(gs12)  
     || rarea graph07_imp imp_CI07_u Num_Countries_imp, color(gs12) 
     || line graph07_imp Num_Countries_imp, sort color(blue) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries from which firm sources")
           legend(order(3 "Premium" 2 "95% CI" ) position(4) ring(0))
	   graphregion(color(white));	
*graph export $output/premia_imp.png, replace	 ; 
#delimit cr


*Export premia
#delimit ;
  twoway rarea graph07_exp exp_CI07_l Num_Countries_exp, color(gs12)  
     || rarea graph07_exp exp_CI07_u Num_Countries_exp, color(gs12) 
     || line graph07_exp Num_Countries_exp, sort color(blue) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries to which firm exports")
           legend(order(3 "Premium" 2 "95% CI" ) position(4) ring(0))
	   graphregion(color(white));	
*graph export $output/premia_exp.png, replace	 ; 
#delimit cr

*FIGURE 2 - IMPORTS
#delimit ;
  twoway rarea graph07_imp imp_CI07_l Num_Countries_imp, color(gs12)  
     || rarea graph07_imp imp_CI07_u Num_Countries_imp, color(gs12) 
     || line graph07_imp Num_Countries_imp, sort color(blue) yaxis(1) 
     || rarea graph07_imp_mne imp_mne_CI07_l Num_Countries_imp_mne, color(gs12)  
     || rarea graph07_imp_mne imp_mne_CI07_u Num_Countries_imp_mne, color(gs12) 
     || line graph07_imp_mne Num_Countries_imp_mne, sort color(red) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   title("Sales Premia and Number of Source Countries") 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries from which firm sources")
           legend(order(3 "Importer" 6 "with MNE Control") position(4) ring(0))
	   graphregion(color(white));	
graph export $output/FIGURE_1_IMPORTS.png, replace	 ; 
#delimit cr
 
*FIGURE 2 - EXPORTS
 #delimit ;
  twoway rarea graph07_exp exp_CI07_l Num_Countries_exp, color(gs12)  
     || rarea graph07_exp exp_CI07_u Num_Countries_exp, color(gs12) 
     || line graph07_exp Num_Countries_exp, sort color(blue) yaxis(1) 
     || rarea graph07_exp_mne exp_mne_CI07_l Num_Countries_exp_mne, color(gs12)  
     || rarea graph07_exp_mne exp_mne_CI07_u Num_Countries_exp_mne, color(gs12) 
     || line graph07_exp_mne Num_Countries_exp_mne, sort color(red) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   title("Sales Premia and Number of Export Countries") 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries to which firm sells")
           legend(order(3 "Exporter" 6 "with MNE Control") position(4) ring(0))
	   graphregion(color(white));	
graph export $output/FIGURE_1_EXPORTS.png, replace	 ; 
#delimit cr 
}

 
*manuf affiliates		
replace man_aff_countries=0 if man_aff_countries==.	
capture drop aff_d07*
	forvalues i=1/10 {
		gen aff_d07_`i'=1 if man_aff_countries>=`i' 
		replace aff_d07_`i'=0 if man_aff_countries<`i' 
		}

areg log_sales aff_d07*, a(naics4)  
make_fig aff aff 10 

*FIGURE 2 - AFFILIATES
quietly { 
#delimit ;
  twoway rarea graph07_aff aff_CI07_l Num_Countries_aff, color(gs12)  
     || rarea graph07_aff aff_CI07_u Num_Countries_aff, color(gs12) 
     || line graph07_aff Num_Countries_aff, sort color(blue) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   xlabel(1(1)10)
	   xtitle("Minimum number of countries in which firm assembles")
           legend(order(3 "Premium" 2 "95% CI" ) position(4) ring(0))
	   graphregion(color(white));	
graph export $output/FIGURE_1_AFFILIATES.png, replace	 ; 
 

  twoway rarea graph07_imp imp_CI07_l Num_Countries_imp, color(gs12)  
     || rarea graph07_imp imp_CI07_u Num_Countries_imp, color(gs12) 
     || line graph07_imp Num_Countries_imp, sort color(red) lpattern(longdash_dot) yaxis(1) 
     || rarea graph07_aff aff_CI07_l Num_Countries_aff, color(gs12)  
     || rarea graph07_aff aff_CI07_u Num_Countries_aff, color(gs12) 
     || line graph07_aff Num_Countries_aff, sort color(blue) yaxis(1)   
     || rarea graph07_exp exp_CI07_l Num_Countries_exp, color(gs12)  
     || rarea graph07_exp exp_CI07_u Num_Countries_exp, color(gs12) 
     || line graph07_exp Num_Countries_exp, sort color(green) lpattern(shortdash)  yaxis(1)  
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7) 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries in which firm does activity") 
           legend(order(6 "Assembles" 3 "Imports" 9 "Exports" ) position(4) ring(0) rows(1)) 
	   graphregion(color(white));	   
*graph export $output/premia_all.png, replace	 ;  
 

  twoway rarea graph07_imp_mne imp_mne_CI07_l Num_Countries_imp_mne, color(gs12)  
     || rarea graph07_imp_mne imp_mne_CI07_u Num_Countries_imp_mne, color(gs12) 
     || line graph07_imp_mne Num_Countries_imp_mne, sort color(red) lpattern(longdash_dot) yaxis(1) 
     || rarea graph07_aff aff_CI07_l Num_Countries_aff, color(gs12)  
     || rarea graph07_aff aff_CI07_u Num_Countries_aff, color(gs12) 
     || line graph07_aff Num_Countries_aff, sort color(blue) yaxis(1)   
     || rarea graph07_exp_mne exp_mne_CI07_l Num_Countries_exp_mne, color(gs12)  
     || rarea graph07_exp_mne exp_mne_CI07_u Num_Countries_exp_mne, color(gs12) 
     || line graph07_exp_mne Num_Countries_exp_mne, sort color(green) lpattern(shortdash) yaxis(1)  
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7) 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries in which firm does activity") 
           legend(order( 6 "Assembles" 3 "Imports" 9 "Exports") position(4) ring(0) rows(1)) 
	   graphregion(color(white));	   
*graph export $output/premia_all2.png, replace	 ; 
#delimit cr 
}

******


***Output to disclose********

*Rounds variables to disclose 
do $disc_programs/rounding_4sigdig_v2.do graph07_imp se07_imp graph07_imp_mne se07_imp_mne graph07_exp se07_exp graph07_exp_mne se07_exp_mne 

do $disc_programs/rounding_4sigdig_v2.do graph07_aff se07_aff


format %9.6fc graph07_imp se07_imp graph07_imp_mne se07_imp_mne graph07_exp se07_exp graph07_exp_mne se07_exp_mne graph07_aff se07_aff

export excel Num_Countries_imp graph07_imp se07_imp graph07_imp_mne se07_imp_mne graph07_exp se07_exp graph07_exp_mne se07_exp_mne ///
	graph07_aff se07_aff ///
	using $output/afft_restat_01.xls if Num_Countries_imp~=. & Num_Countries_imp~=0, sheetreplace sheet(FIGURE_1_data) firstrow(varlabels)  keepcellfmt
	

******************************************************	

 /*
 *Check Lincom figures
 preserve
 
import excel using $output/afft_restat_01.xls,  sheet(FIGURE_1_data) firstrow  clear
	
 foreach vname in imp imp_mne exp exp_mne aff {
   capture drop `vname'_CI07_l
   gen `vname'_CI07_l=graph07_`vname'-1.96*se07_`vname'
   capture drop `vname'_CI07_u
   gen `vname'_CI07_u=graph07_`vname'+1.96*se07_`vname'
   }
   
   #delimit ;
  twoway rarea graph07_aff aff_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_aff aff_CI07_u Num_Countries, color(gs12) 
     || line graph07_aff Num_Countries, sort color(blue) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   xlabel(1(1)10)
	   xtitle("Minimum number of countries in which firm assembles")
           legend(order(3 "Premium" 2 "95% CI" ) position(4) ring(0))
	   graphregion(color(white));
	   
  #delimit ;	   
   twoway rarea graph07_imp imp_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_imp imp_CI07_u Num_Countries, color(gs12) 
     || line graph07_imp Num_Countries, sort color(red) lpattern(longdash_dot) yaxis(1) 
     || rarea graph07_aff aff_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_aff aff_CI07_u Num_Countries, color(gs12) 
     || line graph07_aff Num_Countries, sort color(blue) yaxis(1)   
     || rarea graph07_exp exp_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_exp exp_CI07_u Num_Countries, color(gs12) 
     || line graph07_exp Num_Countries, sort color(green) lpattern(shortdash)  yaxis(1)  
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7) 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries in which firm does activity") 
           legend(order(6 "Assembles" 3 "Imports" 9 "Exports" ) position(4) ring(0) rows(1)) 
	   graphregion(color(white));	   

 #delimit ;
  twoway rarea graph07_imp imp_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_imp imp_CI07_u Num_Countries, color(gs12) 
     || line graph07_imp Num_Countries, sort color(blue) yaxis(1) 
     || rarea graph07_imp_mne imp_mne_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_imp_mne imp_mne_CI07_u Num_Countries, color(gs12) 
     || line graph07_imp_mne Num_Countries_, sort color(red) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   title("Sales Premia and Number of Source Countries") 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries from which firm sources")
           legend(order(3 "Importer" 6 "with MNE Control") position(4) ring(0))
	   graphregion(color(white));	
graph export $output/premia_imp2.png, replace	 ; 

 
 #delimit ;
  twoway rarea graph07_exp exp_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_exp exp_CI07_u Num_Countries, color(gs12) 
     || line graph07_exp Num_Countries, sort color(blue) yaxis(1) 
     || rarea graph07_exp_mne exp_mne_CI07_l Num_Countries, color(gs12)  
     || rarea graph07_exp_mne exp_mne_CI07_u Num_Countries, color(gs12) 
     || line graph07_exp_mne Num_Countries, sort color(red) yaxis(1) 
     ||,
	   ytitle( "Premium", axis(1))  ylabel(0(1)7)
	   title("Sales Premia and Number of Export Countries") 
	   xlabel(1(2)26)
	   xtitle("Minimum number of countries to which firm sells")
           legend(order(3 "Exporter" 6 "with MNE Control") position(4) ring(0))
	   graphregion(color(white));	
graph export $output/premia_exp2.png, replace	 ; 	

restore   
   */
	  	   
	   
	   

 ***Make histogram of no. of import country bins
 save $data/temp.dta, replace
 

 
 use $data/temp.dta, clear
 
 foreach val in imp exp {
 	capture drop `val'_bin
	*gen `val'_bin=1 if number_`val'ort_countries==1
	gen `val'_bin=1 if number_`val'ort_countries>1 & number_`val'ort_countries<=5
	replace `val'_bin=2 if number_`val'ort_countries>5 & number_`val'ort_countries<=10
	replace `val'_bin=3 if number_`val'ort_countries>10 & number_`val'ort_countries<=15
	replace `val'_bin=4 if number_`val'ort_countries>15 & number_`val'ort_countries<=25
	replace `val'_bin=5 if number_`val'ort_countries>25 & number_`val'ort_countries<=35
	replace `val'_bin=6 if number_`val'ort_countries>35 & number_`val'ort_countries<=45
	replace `val'_bin=7 if number_`val'ort_countries>45 & number_`val'ort_countries~=.
	}

label define bins 1 "2-5" 2 "6-10" 3 "11-15" 4 "16-25" 5 "26-35" 6  "36-45" 7 "46+"

label values imp_bin exp_bin bins
 
tab imp_bin, miss

tab imp_bin mne_status, miss 
tab exp_bin mne_status, miss 

tab imp_bin mne_simp, miss 
tab exp_bin mne_simp, miss 

 
gen firms=1

preserve
collapse (sum) imp_val firms, by(imp_bin mne_simp)

gen double imports=imp_val/1000000

rename firms firms_imp

reshape wide imports imp_value firms, i(imp_bin) j(mne_simp) string


graph bar (asis) importsDOM importsFOR importsMNE , over(imp_bin) stack  ytitle("Imports (Billion USD)") ///
	bar(1, fcolor(navy) lcolor(navy) fintensity(80)) ///
	bar(2, fcolor(red) lcolor(red) fintensity(80)) ///
	bar(3, fcolor(blue) lcolor(blue) fintensity(80)) ///
	legend(order(1 "Domestic Firms" 2 "Foreign MNEs" 3 "US MNEs") rows(1)) ///
	title("US Manufacturers' Imports in 2007") subtitle("By Number of Source Countries and MNE Status of Importer") ///
	graphregion(color(white))
 *graph export $output/FIGURE_2_IMPORTS.png, replace	 


 save $data/temp_import_bins.dta, replace
 
 restore
 
 
 preserve
collapse (sum) exp_val firms, by(exp_bin mne_simp)

gen double exports=exp_val/1000000

reshape wide exports exp_value firms, i(exp_bin) j(mne_simp) string


graph bar (asis) exportsDOM exportsFOR exportsMNE , over(exp_bin) stack  ytitle("Imports (Billion USD)") ///
	bar(1, fcolor(navy) lcolor(navy) fintensity(80)) ///
	bar(2, fcolor(red) lcolor(red) fintensity(80)) ///
	bar(3, fcolor(blue) lcolor(blue) fintensity(80)) ///
	legend(order(1 "Domestic Firms" 2 "Foreign MNEs" 3 "US MNEs") rows(1)) ///
	title("US Manufacturers' Exports in 2007") subtitle("By Number of Source Countries and MNE Status of Importer") ///
	graphregion(color(white))
 * graph export $output/FIGURE_2_EXPORTS.png, replace	 

 
 *Merge in the import data
 rename exp_bin imp_bin
 merge 1:1 imp_bin using $data/temp_import_bins.dta
 
 drop if imp_bin==.
 rename imp_bin country_count_bin
 drop _merge
 

 
 *Round
 
 do $disc_programs/rounding_4sigdig_v2.do exportsDOM exportsFOR exportsMNE importsDOM importsFOR importsMNE 
 
 format %9.2fc exportsDOM exportsFOR exportsMNE importsDOM importsFOR importsMNE
 
gen country_count_bin_numeric=country_count_bin
 
export excel country_count_bin_numeric country_count_bin exportsDOM exportsFOR exportsMNE importsDOM importsFOR importsMNE  ///
	using $output/afft_restat_01.xls , sheetreplace sheet(FIGURE_2_data) firstrow(varlabels)
 


 
import excel using $output/afft_restat_01.xls , sheet(FIGURE_2_data)  clear firstrow


*CHECK DISCLOSED FIGURES
/*
label define bins 1 "2-5" 2 "6-10" 3 "11-15" 4 "16-25" 5 "26-35" 6  "36-45" 7 "46+"

label values country_count_bin_num bins
 
graph bar (asis) DOMexports FORexports MNEexports , over(country_count_bin_num) stack  ytitle("Exports (Billion USD)") ///
	bar(1, fcolor(navy) lcolor(navy) fintensity(80)) ///
	bar(2, fcolor(red) lcolor(red) fintensity(80)) ///
	bar(3, fcolor(blue) lcolor(blue) fintensity(80)) ///
	legend(order(1 "Domestic Firms" 2 "Foreign MNEs" 3 "US MNEs") rows(1)) ///
	title("US Manufacturers' Exports in 2007") subtitle("By Number of Destination Countries and MNE Status of Exporter") ///
	graphregion(color(white))
 graph export $output/no_exp_ctrs.png, replace	
 
 
graph bar (asis) DOMimports FORimports MNEimports , over(country_count_bin_num)  stack  ytitle("Imports (Billion USD)") ///
	bar(1, fcolor(navy) lcolor(navy) fintensity(80)) ///
	bar(2, fcolor(red) lcolor(red) fintensity(80)) ///
	bar(3, fcolor(blue) lcolor(blue) fintensity(80)) ///
	legend(order(1 "Domestic Firms" 2 "Foreign MNEs" 3 "US MNEs") rows(1)) ///
	title("US Manufacturers' Imports in 2007") subtitle("By Number of Source Countries and MNE Status of Exporter") ///
	graphregion(color(white))
 graph export $output/no_imp_ctrs.png, replace	

 */
 restore

 
 



